package com.jwm.power.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.jwm.power.mapper.OperateMapper;
import com.jwm.power.model.common.EnumOperate;
import com.jwm.power.model.common.EnumResult;
import com.jwm.power.model.entity.Deptjob;
import com.jwm.power.model.entity.Logs;
import com.jwm.power.model.entity.Users;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.repository.JobsRepository;
import com.jwm.power.repository.LogsRepository;
import com.jwm.power.service.CompService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestDataBinder;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CompServiceImpl implements CompService {
    @Autowired
    private JobsRepository jobsRepository;
    @Autowired
    private LogsRepository logsRepository;
    @Autowired
    private OperateMapper operateMapper;

    //  设置职务
    @Override
    public ResponseData SaveJobs(HttpServletRequest request) {
        try {
            Users users = (Users) StpUtil.getSession().get("users");
            Deptjob model_New = new Deptjob();
            ServletRequestDataBinder binder =
                    new ServletRequestDataBinder(model_New);
            binder.bind(request);
            if (model_New.getParentcode() == null || model_New.getParentcode().equals("")) {
                model_New.setParentcode("00");
            }
            Integer operationType = 0;
            if (model_New.getJobid() != null && model_New.getJobid() > 0) {
                operationType = 1;
                //  同级职务名称不能相同
                List<Deptjob> deptjobList = jobsRepository.findByParentcodeAndJobnameAndJobidNot(model_New.getParentcode(), model_New.getJobname(), model_New.getJobid());
                if (deptjobList.size() > 0) {
                    return new ResponseData(EnumResult.DEPTJOB_FAIL_FNAME, null);
                }
            } else {
                String maxCode = jobsRepository.getMaxCode(model_New.getParentcode());
                maxCode = maxCode != null ? maxCode : "0";
                String parentCode = model_New.getParentcode().equals("00") ? "" : model_New.getParentcode();
                model_New.setJobcode(parentCode + String.format("%02d", Integer.parseInt(maxCode) + 1));
                //  同级职务名称不能相同
                List<Deptjob> deptjobList = jobsRepository.findByParentcodeAndJobname(model_New.getParentcode(), model_New.getJobname());
                if (deptjobList.size() > 0) {
                    return new ResponseData(EnumResult.DEPTJOB_FAIL_FNAME, null);
                }
            }
            jobsRepository.save(model_New);
            //  添加日志
            Logs log = Ut.SaveLog("jobs", model_New.getJobname(), operationType == 1 ? EnumOperate.EDIT : EnumOperate.ADD, users);
            logsRepository.save(log);
            return new ResponseData(EnumResult.SUCCESS, model_New);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
    }

    //  设置职务菜单
    @Override
    public ResponseData SaveJobMenus(HttpServletRequest request) {
        try {
            Users users = (Users) StpUtil.getSession().get("users");
            String moduleItems = request.getParameter("moduleItems");//菜单编码集合
            String jobCode = request.getParameter("jobCode");//职务编码
            Map<String, Object> map = new HashMap<>();
            map.put("tName", "JobMenus");
            map.put("strWhere", "jobcode ='" + jobCode + "'");
            operateMapper.deleteDefined(map);   //  删除原有权限
            //  添加新权限
            map = new HashMap<>();
            String strWhere = "select '" + jobCode + "',menucode,authname from menus";
            strWhere += " where charindex(  menus.menucode,'" + moduleItems + "') > 0";
            map.put("strWhere", strWhere);
            operateMapper.insertBatchJobMenus(map);   //  批量增加职务权限
            //  添加日志
            Logs log = Ut.SaveLog("jobMenus", moduleItems, EnumOperate.EDIT, users);
            logsRepository.save(log);
            return new ResponseData(EnumResult.SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
    }

}
